// DownloadData.cpp : implementation file
//
#include "stdafx.h"
#include "resource.h"
#include "DownloadData.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif


/////////////////////////////////////////////////////////////////////////////
// CDownloadData property page

IMPLEMENT_DYNCREATE(CDownloadData, CPropertyPage)

CDownloadData::CDownloadData() : CPropertyPage(CDownloadData::IDD)
{
	//{{AFX_DATA_INIT(CDownloadData)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}

CDownloadData::~CDownloadData()
{
}

void CDownloadData::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDownloadData)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDownloadData, CPropertyPage)
	//{{AFX_MSG_MAP(CDownloadData)
	ON_BN_CLICKED(IDC_SavedFile, OnSavedFile)
	ON_BN_CLICKED(IDC_StopDownload, OnStopDownload)
	ON_BN_CLICKED(IDC_StartDownload, OnStartDownload)
	ON_WM_CLOSE()
	ON_MESSAGE (WM_DOWNLOAD_COMPLETE, OnDownloadExit)
	ON_MESSAGE (WM_DOWNLOAD_PROGRESS, OnDownloadProgress)
	ON_MESSAGE (WM_WRITE_LOG_FILE, OnWriteLogToFile)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDownloadData message handlers

BOOL CDownloadData::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();
	
	if (!m_Download.Create(this))
		g_GlobalObj.UIWriteLog("m_Download.Create Error");
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CDownloadData::OnSavedFile() 
{
	if (!m_Download.ChooseSavedFileName())
		g_GlobalObj.UIWriteLog("m_Download.ChooseSavedFileName Error");
}

BOOL CDownloadData::OnKillActive() 
{
	CString szMsg;
	
	if (m_Download.IfDownloading())
	{
		szMsg.LoadString(IDS_StopDownloadBeforeChangePage);
		AfxMessageBox(szMsg);
//		g_GlobalObj.UIWriteLog("Please stop download first before changing page");
		return false;
	}
/*
	if(!m_Download.UpdateMemberVariables())
		g_GlobalObj.UIWriteLog("m_Download.UpdateUI Error");
*/
	return CPropertyPage::OnKillActive();
}

BOOL CDownloadData::OnSetActive() 
{
	if(!m_Download.UpdateUI())
		g_GlobalObj.UIWriteLog("m_Download.UpdateUI Error");
	
	return CPropertyPage::OnSetActive();
}

void CDownloadData::OnStopDownload() 
{
	if(!m_Download.StopDownload())
		g_GlobalObj.UIWriteLog("m_Download.StartDownload Error");
	
}

void CDownloadData::OnStartDownload() 
{
	if(!m_Download.StartDownload())
		g_GlobalObj.UIWriteLog("m_Download.StartDownload Error");
}


void CDownloadData::OnClose() 
{
//	if(!m_Download.CloseWindow())
//		g_GlobalObj.UIWriteLog("m_ConnectToMap.CloseWindow Error");
	
	CPropertyPage::OnClose();
}

LONG CDownloadData::OnDownloadExit (UINT wParam, LONG lParam)
{
	TRACE(_T("receive download thread exit message\n"));
	if (!m_Download.DownloadThreadExit())
		g_GlobalObj.UIWriteLog("m_Download.DownloadThreadExit Error");

	return true;
}

LONG CDownloadData::OnDownloadProgress (UINT wParam, LONG lParam)
{
	if (!m_Download.DownloadProgress((int) lParam))
		g_GlobalObj.UIWriteLog("m_Download.DownloadProgress Error");

	return true;
}

LONG CDownloadData::OnWriteLogToFile (UINT wParam, LONG lParam)
{
	//for test
	TRACE(_T("Receive wm_writelog msg\n"));
	if (!m_Download.WaitForWriteLog())
		g_GlobalObj.UIWriteLog("m_Download.WaitForWriteLog Error");

	return true;
}
